import { PrismaClient } from '@prisma/client'
import { getQuery } from 'h3'
const prisma = new PrismaClient()

export default defineEventHandler(async (event) => {
    const { page, limit, topicId} = await getQuery(event)
    const count = await prisma.post.count({where:{deletedAt:null}})
    const data = await prisma.post.findMany({
        where: {topicId: Number(topicId), deletedAt:null},
        skip: (Number(page) - 1)*Number(limit),
        take: Number(limit),
        select: {
            id: true,
            score: true,
            speed: true,
            createdAt: true,
            user: {
                select: {
                    nickname: true,
                    phone: true,
                    company: true,
                }
            },
            topic: {
                select: {
                    id: true,
                    title: true,
                    limit: true,
                    quantity: true,
                }
            },
        }
    })
    return {
        count,
        data,
    }
})
